package org.lwxyz.service;

import com.google.common.collect.Lists;
import org.lwxyz.entity.Redis;
import org.lwxyz.utils.JsonUtils;
import org.lwxyz.utils.StringUtil;
import org.lwxyz.utils.redis.JedisUtil;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * @author yuwei
 * @version 1.0-SNAPSHOT
 * @description
 * @date
 */
@Service
public class RedisService {
    /**
     * 获取
     * @return
     */
    public String redis(){
        Jedis jedis = JedisUtil.getDefaultJedis();
        List<Redis> redisList = Lists.newArrayList();
        int i = 0;
        while (true) {
            Object name = jedis.get("countdown_name_" + i);
            Object value = jedis.get("countdown_value_" + i);
            System.out.println("name:" + name);
            if (name == null || StringUtil.isBlank(name.toString())) {
                break;
            }
            Redis redis = new Redis(name.toString(), value.toString());
            redisList.add(redis);
            i++;
        }
        jedis.close();
        return JsonUtils.obj2JsonString(redisList);
    }

    /**
     * 添加
     * @param key
     * @param value
     * @return
     */
    public String add(final String key, final String value){
        Jedis jedis = JedisUtil.getDefaultJedis();
        if (jedis.exists("countdown_name_9")) {
            return "no";
        }
        int count = count();
        jedis.set("countdown_name_" + count, key);
        jedis.set("countdown_value_" + count, value);
        jedis.close();
        return "ok";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public String delete(final String id){
        Jedis jedis = JedisUtil.getDefaultJedis();
        jedis.del("countdown_name_" + id);
        jedis.del("countdown_value_" + id);
        jedis.close();
        sort();
        return "ok";
    }

    /**
     * 排序
     */
    public void sort(){
        // 查出全部删除
        List<Redis> redisList = query();
        // 删除全部
        for (int i = 0; i < 10; i++) {
            delete("" + i);
        }
        // 将之前取出的结果重新有序保存
        for (Redis redis: redisList) {
            System.out.println(redis.getKey() + "-----" + redis.getValue().toString());
            add(redis.getKey(), redis.getValue().toString());
        }
    }

    /**
     * 获取所有的数据
     * @return
     */
    public List<Redis> query(){
        Jedis jedis = JedisUtil.getDefaultJedis();
        List<Redis> redisList = Lists.newArrayList();
        for (int i = 0; i < 10; i++) {
            Object name = jedis.get("countdown_name_" + i);
            Object value = jedis.get("countdown_value_" + i);
            System.out.println(i + " name:" + name);
            if (name != null && StringUtil.isNotBlank(name.toString())) {
                Redis redis = new Redis(name.toString(), value.toString());
                redisList.add(redis);
            }
        }
        jedis.close();
        return redisList;
    }

    /**
     * 获取这个类型的可以的总和
     * @return
     */
    public int count(){
        Jedis jedis = JedisUtil.getDefaultJedis();
        int count = 0;
        while (true) {
            Object name = jedis.get("countdown_name_" + count);
            if (name == null || StringUtil.isBlank(name.toString())) {
                break;
            }
            count++;
        }
        jedis.close();
        return count;
    }
}
